<?php 
defined('IN_ADMIN') or exit('No permission resources.');
include $this->admin_tpl('header');
?> 

<script language="javascript" type="text/javascript" src="<?php echo JS_PATH;?>commoncheck.js"></script>
<script language="javascript" type="text/javascript" src="<?php echo JS_PATH?>DatePicker/WdatePicker.js"></script>
<script language="javascript" type="text/javascript" src="<?php echo JS_PATH;?>open_window.class.js"></script>
<body onload="opportunitycheck()">
<div class="txt_head_frame">
	<span class="txt_frame_title">增加销售机会</span>
</div>
<div id="main_content">
<form name="myform" action="?m=sale&c=opportunitymanage&a=edit" method="post" id="myform">
<input type="hidden" name="info[customer_id]" id="_N_id_" value="<?php echo $customer_id?>"/>
<input type="hidden" name="info[opportunity_id]" id="opportunity_id" value="<?php echo $opportunity_id?>/>
<input type="hidden"  id="_contact_id_"/>
<div class="txt_content_frame">
	<div class="txt_content_box">
		<div class="txt_content_left">
			<div class="txt_text_left">销售机会名称:<span>[*]</span></div>
			<div class="txt_left_input"><input name="info[opportunity_name]" type="text" class="txt" id="opportunity_name" value="<?php echo $opportunity_name?>"/></div><span id="validateopportunity_name" class="validate"></span><br/><br/>
			<div class="txt_text_left">对应客户:<span>[*]</span></div>
			<div class="txt_left_input"><input  type="text" class="txt" disabled="disabled" id="_N_name_" value="<?php echo $customer_name['customer_name']?>"/><a onclick="javascript:Open_window.winOpen(set_ajx_url('index.php?m=account&c=customermanage&a=search'))" href="javascript:void(0)"><img src="images/detail.png"></a></div><br/><br/>
            <div class="txt_text_left">负责联系人:</div>
			<div class="txt_left_input">
            <select name="info[contact_id]" id= 'contact_id' class="select" onclick="get_contact()">
            <option value="<?php echo $contact_id?>"><?php echo $contact_name['contact_name']?></option>
			</select>
			</div><br/><br/>
			<div class="txt_text_left">发现时间:<span>[*]</span></div>
			<div class="txt_left_input"><input name="info[opportunity_data]" type="text" class="txt choose_date" id="opportunity_data" value="<?php echo date('Y-m-d',$opportunity_data)?>" onClick="WdatePicker()"/></div><br/><br/>
			<div class="txt_text_left">来源:</div>
			<div class="txt_left_input">
            <select name="info[opportunity_source]" class="select">
             <option value=""></option>
            <?php 
            	foreach ($option_cache['opportunity_source'] as $key => $value) {
            ?>
			<option value="<?php echo $key?>"<?php echo $key == $opportunity_source ? 'selected' : ''?>><?php echo $value?></option>
			<?php 
            	}
			?>
			</select>
			</div><br/><br/> 
		</div>
		<div class="txt_content_right">
			<div class="txt_text_left">签约时间:</div>
			<div class="txt_left_input"><input name="info[opport_sign_data]" type="text" class="txt choose_date" id="opport_sign_data" value="<?php if($opport_sign_data != 0) echo date('Y-m-d',$opport_sign_data)?>" onClick="WdatePicker()"/></div><br/><br/>  
			<div class="txt_text_left">预期金额:</div>
			<div class="txt_left_input"><input name="info[prev_sum]" type="text" class="txt" id="prev_sum" value="<?php echo $prev_sum?>"/></div><br/><br/>
			<div class="txt_text_left">阶段:<span>[*]</span></div>
			<div class="txt_left_input">
            <select name="info[opportunity_stage]" class="select">
            <?php 
            	foreach ($option_cache['opportunity_stage'] as $key => $value) {
            ?>
			<option value="<?php echo $key?>" <?php echo $key == $opportunity_stage ? 'selected' : ''?>><?php echo $value?></option>
			<?php 
            	}
			?>
			</select>
			</div><br/><br/> 
			<div class="txt_text_left">可能性:</div>
			<div class="txt_left_input">
            <select name="info[opport_possibility]" class="select">
             <option value=""></option>
            <?php 
            	foreach ($option_cache['opport_possibility'] as $key => $value) {
            ?>
			<option value="<?php echo $key?>" <?php echo $key == $opport_possibility ? 'selected' : ''?>><?php echo $value?></option>
			<?php 
            	}
			?>
			</select>
			</div><br/><br/> 
			<div class="txt_text_left">状态:<span>[*]</span></div>
			<div class="txt_left_input">
            <select name="info[opport_status]" class="select">
            <?php 
            	foreach ($option_cache['opport_status'] as $key => $value) {
            ?>
			<option value="<?php echo $key?>" <?php echo $key == $opport_status ? 'selected' : ''?>><?php echo $value?></option>
			<?php 
            	}
			?>
			</select>
			</div><br/><br/> 		
	</div>
		<div class="txt_txtarea">
			<div class="txt_txtarea_text">备注：</div>
			<div class="txt_txtarea_input1"><textarea name="info[remark]" cols="50" rows="4" class="txt_txtarea_font"></textarea></div>
			<div class="txt_txtarea_text">服务项目：</div>	
			<table border="0" cellpadding="0" class="main_table" style="width:50%;background:#f8f8ff;">
				<thead>
					<tr class="main_table_header">
						<th width="4%">序号</th>
						<th width="15%">服务内容</th>
						<th width="15%">价格</th>
						<th width="10%">操    作        <a href="javascript:addItem()" title="增加"><img src="images/Rolesadd.gif"/></a></th>					
					</tr>
				</thead>
				<tbody id = "main_tbody" >
					<?php if(is_array($quotes)) { $k = 1; foreach ($quotes as $quote){?>
					<tr><input type="hidden" name="quote[<?php echo $k?>][quote_id]" value="<?php echo $quote['quote_id']?>"><td><?php echo $k?></td><td><input type="text" class="txt" name="quote[<?php echo $k?>][quote_name]" value="<?php echo $quote['quote_name']?>"></input></td><td><input type="text" class="txt" name="quote[<?php echo $k?>][price]" onblur="calcTotal()" value="<?php echo $quote['price']?>"></input></td><td><a href="javascript:;" onclick="descItem(this,<?php echo $k?>);" title="删除"><img src="images/view_-.gif"/></a></td></tr>
					<?php $k++; } }?>
				</tbody>
			</table>
		</div>
	</div>
</div>

<div class="txt_btn">
	<input name="dosubmit" type="submit" class="txt_btn_a" value=" 保存 " />
</div>
</form>
</div>
<div id='test'></div>
</body>
<script type="text/javascript">

var Open_window = new Open_window();
function opportunitycheck(){
	$('#opportunity_name').blur(function(){
		var validate = $('#validateopportunity_name');
		if(isNotNull($(this).val())){	
			validate.html('<img src="images/true.gif" width=16 heigth=16>');
		} else {
			validate.html('<img src="images/false.gif" width=16 heigth=16>不能为空');	
		}
	});
}
function get_contact(){
	var customerid = $('#_N_id_').val();
	if(this.last_customerid != customerid){
		if(customerid == '') {alert("相关客户不能为空");return ;}
		$.ajax({
			type:'post',
			url:set_ajx_url("?m=sale&c=opportunitymanage&a=get_contact"),
			data:'customer_id='+customerid,
			dataType:'json',
			context:this,
			success:function(data){
				var html = '<option value=""></option>';
				$.each(data,function(id,value){
					html += "<option value='"+value['contact_id']+"'>"+value['contact_name']+"</option>";
				});
				$('#contact_id').empty();
				$('#contact_id').append(html);
			},
			error:function(){
				alert('get_contact,error');
			}
		});
		this.last_customerid = $('#_N_id_').val(); 
		}	
}

function addItem(){
	var n = $('#main_tbody ').find('input[type=text]').length/2+1;
	var newOption = '<tr><td>'+n+'</td><td><input type="text" class="txt" name="quote['+n+'][quote_name]"></input></td><td><input type="text" class="txt" name="quote['+n+'][price]" onblur="calcTotal()"></input></td><td><a href="javascript:;" onclick="descItem(this,'+n+');" title="删除"><img src="images/view_-.gif"/></a></td></tr>';
	$('#main_tbody').append(newOption);
}
function descItem(a,id){
	$(a).parent().parent().append('<input type="hidden" name="quote['+id+'][del]" value="1"/>');
	$(a).parent().parent().fadeOut();
}
function calcTotal(){
	var sum = 0;
	$("input[name$='[price]']").each(function(){
		sum = sum+Number($(this).val());		
	});
	if($('#prev_sum').val() < sum){
		$('#prev_sum').val(sum);
	}
}
</script>
</html>